<template>
	<div class="content-main" :class="[`bg-${getDarkTheme?'black':'white'}`]">
		<basicTable ref="resourceListRef"
		 :canResize="true" :schemas="schemas"
		  :columns="cols" :pagination="true" :exportInfo="exportInfo"
		   :request="searchList">
			<template #tableTitle class="table-title">
				<n-dropdown trigger="hover" :options="addOptions" @select="dropDownSelect($event, 'add')">
					<n-button type="primary"
						><i class="iconfont icon-add"></i>
						新增
					</n-button>
				</n-dropdown>
				<n-button @click="deleteResource()" :disabled="!getSelectRowLength"
					><i class="iconfont icon-delete"></i>
					删除
				</n-button>
				<n-dropdown trigger="hover" :options="downloadImportOptions" @select="dropDownSelect($event, 'downloadImport')">
					<n-button type="primary"
						><i class="iconfont icon-download"></i>
						下载导入模板
					</n-button>
				</n-dropdown>
				<n-dropdown trigger="hover" :options="importOptions" @select="dropDownSelect($event, 'import')">
					<n-button type="primary">
						<i class="icon-export-template iconfont"></i>
						导入资源信息
					</n-button>
				</n-dropdown>
				<n-button @click="addBrevity()" :disabled="!getSelectRowLength">
					<i class="iconfont icon-zancunjia"></i>
					添加到暂存架
				</n-button>
				<!--				<n-dropdown-->
				<!--					trigger="hover"-->
				<!--					:options="importResourceOptions"-->
				<!--					@select="dropDownSelect($event, 'importResource')">-->
				<!--					<n-button type="primary">-->
				<!--						<i class="iconfont icon-export-template"></i>-->
				<!--						导入资源文件-->
				<!--					</n-button>-->
				<!--				</n-dropdown>-->
				<n-button type="primary" @click="dropDownSelect($event, 'importResource')">
					<i class="iconfont icon-export-template"></i>
					导入资源文件
				</n-button>
			</template>
		</basicTable>
		<h-upload-modal ref="uploadModal" :uploadPath="uploadPath" @submit="getUploadObj"></h-upload-modal>
	</div>
</template>
<script lang="ts" setup>
	import { addTemporaryShelf, batchImportPublishingResourceFile, importOriginalResource, importPublishResource, queryResourceList } from '@/api/resource/user';
	import { filterRenderType } from '@/components/Table/src/enum/filterRenderType';
	import { cloneDeep } from 'lodash-es';
	import dayjs from 'dayjs';
	import enums from '@/entity/global';
	import commonUtil from '@/utils/common-util';
	import { downloadFile } from '@/utils/downloadByUrl';
	import { upload } from '@/components/hUpload/upload-service';
  import {useDesignSetting} from "@/hooks/setting/useDesignSetting";
  const { getDarkTheme } = useDesignSetting();
	const router = useRouter();
	const resourceListRef = ref(null);
	const uploadModal = ref(null);
	let uploadPath = reactive({});
	let listParams = reactive({});

	let secondClassList = [];
	let originalClassLists = JSON.parse(localStorage.getItem('originalClassList'));
	let publishingClassLists = JSON.parse(localStorage.getItem('publishingClassList'));
	let originalFirstClassList = cloneDeep(originalClassLists || []).map((item) => {
		secondClassList = secondClassList.concat(item.children);
		delete item.children;
		return item;
	});
	let publishingFirstClassList = cloneDeep(publishingClassLists || []).map((item) => {
		secondClassList = secondClassList.concat(item.children);
		delete item.children;
		return item;
	});

	const exportInfo = {
		exportType: enums.exportTypeEnum.resource,
		columnName: enums.columnName.resource,
		exportAllUrl: `/iop-resourcelib-service/resource/exportExcelAll`,
		exportSelectUrl: `/iop-resourcelib-service/resource/exportExcelById`,
		exportName: '资源信息',
	};

	const addOptions = [
		{
			label: '原创资源',
			key: 'Original',
		},
		{
			label: '出版资源',
			key: 'Publish',
		},
	];
	const downloadImportOptions = [
		{
			label: '下载原创模板',
			key: 0,
			permission: 100004,
		},
		{
			label: '下载出版模板',
			key: 1,
			permission: 100005,
		},
	];
	const importOptions = [
		{
			label: '导入原创资源',
			key: 0,
			permission: 100006,
		},
		{
			label: '导入出版资源',
			key: 1,
			permission: 100007,
		},
	];
	const importResourceOptions = [
		{
			label: '导入出版资源',
			key: 0,
			permission: 100006,
		},
	];

	const schemas = [
		{
			field: 'resourceName',
			component: 'NInput',
			label: '书名查询',
			componentProps: {
				placeholder: '请输入书名',
				onInput: (e: any) => {
					console.log(e);
				},
			},
		},
		{
			field: 'isbnNum',
			component: 'NInput',
			label: 'ISBN查询',
			componentProps: {
				placeholder: '请输入ISBN',
				showButton: false,
				onInput: (e: any) => {
					console.log(e);
				},
			},
		},
		{
			field: 'bookNum',
			component: 'NInput',
			label: '书号查询',
			componentProps: {
				placeholder: '请输入书号',
				showButton: false,
				onInput: (e: any) => {
					console.log(e);
				},
			},
		},
	];
	const cols = [
		{
			type: 'selection',
			fixed: 'left',
		},
		{
			title: '书名',
			key: 'resourceName',
			sorter: true,
			width: 200,
			filterType: filterRenderType.INPUT,
			render: (row, index) => {
				return h(
					'span',
					{
						class: 'open-bookDetail',
						onClick: () => {
							console.log(row.resourceName);
						},
					},
					{ default: () => row.resourceName }
				);
			},
		},
		{
			title: '类型',
			key: 'resourceType',
			sorter: true,
			width: 100,
			filterMultiple: false,
			filterOptions: [
				{
					label: '原创',
					value: '0',
				},
				{
					label: '出版',
					value: '1',
				},
			],
			filterType: filterRenderType.SINGLESELECT,
			render: (row, index) => {
				return h(
					'span',
					{
						class: '',
					},
					{ default: () => (row.resourceType === 1 ? '出版' : '原创') }
				);
			},
		},
		{
			title: 'ISBN',
			key: 'isbnNum',
			width: 150,
			sorter: true,
			filterType: filterRenderType.INPUT,
		},
		{
			title: '书号',
			key: 'bookNum',
			width: 200,
			sorter: true,
			filterType: filterRenderType.INPUT,
		},
		{
			title: '作者笔名',
			key: 'authorPenName',
			width: 120,
			sorter: true,
			filterType: filterRenderType.INPUT,
		},
		{
			title: '一级分类',
			key: 'firstClass',
			width: 150,
			sorter: true,
			filterType: filterRenderType.SINGLESELECT,
			filterOptions: originalFirstClassList.concat(publishingFirstClassList),
		},
		{
			title: '二级分类',
			key: 'secondClass',
			width: 150,
			sorter: true,
			filterType: filterRenderType.SINGLESELECT,
			filterOptions: secondClassList,
		},
		{
			title: '出版社',
			key: 'pressName',
			width: 150,
			sorter: true,
			filterType: filterRenderType.INPUT,
		},
		{
			title: '出版年月',
			key: 'publishDate',
			width: 150,
			sorter: true,
			filterType: filterRenderType.DATERANGEPICKER,
			render: (row, index) => {
				return h(
					'span',
					{
						class: '',
					},
					{
						default: () => (row.publishDate ? dayjs(row.publishDate).format('YYYY-MM-DD') : ''),
					}
				);
			},
		},
		{
			title: '创建日期',
			key: 'insertTime',
			width: 150,
			sorter: true,
			filterType: filterRenderType.DATERANGEPICKER,
			render: (row, index) => {
				return h(
					'span',
					{
						class: '',
					},
					{
						default: () => (row.insertTime ? dayjs(row.insertTime).format('YYYY-MM-DD') : ''),
					}
				);
			},
		},
		{
			title: '操作',
			key: 'operationTemplate',
			width: 100,
			fixed: 'right',
			render(row) {
				return h(
					'span',
					{
						className: 'cursor-pointer flex items-center',
						style: {
							lineHeight: '15px',
						},
					},
					[
						h('i', {
							className: 'icon iconfont icon-edit',
							style: {
								margin: '0 10px 0 5px',
							},
							onClick: () => edit(row),
						}),
						h('i', {
							className: 'icon iconfont icon-delete red-icon',
							onClick: () => deleteRow(row),
						}),
					]
				);
			},
		},
	];
  const $dialog = window['$dialog']
	const dropDownSelect = (e, e2) => {
		if (e2 === 'add') {
			router.push(`/resource/resource-detail/${e}/${e2}`);
		}
		if (e2 === 'downloadImport') {
			if (e === 0) {
				downloadFile(enums.downloadTemplate.originalResource);
			} else {
				downloadFile(enums.downloadTemplate.publishingResource);
			}
		}
		if (e2 === 'import') {
			upload().then(async (file) => {
				if (commonUtil.testFileName('.xlsx/.xls', file[0])) {
					let formData = new FormData();
					formData.set('file', file[0]);
					if (e === 1) {
						await importPublishResource(formData);
					} else {
						await importOriginalResource(formData);
					}
					resourceListRef.value.reload();
				} else {
					$dialog.error({
						title: '警告',
						content: '文件类型不符合',
					});
				}
			});
		}
		if (e2 === 'importResource') {
			const uploadPathList = JSON.parse(sessionStorage.getItem('userInfo')).fileFuncList;
			uploadPath = Object.assign(uploadPath, uploadPathList[uploadPathList.findIndex((item) => item.funcCatagoryCode === 6)]);
			uploadModal.value.showModal();
		}
	};

	const getUploadObj = async (params) => {
		await batchImportPublishingResourceFile(params);
	};

	const edit = (row) => {
		console.log(row);
	};

	const deleteRow = (row) => {
		console.log(row);
	};

	const addBrevity = async () => {
		console.log(resourceListRef.value.getCheckRowKeys());
		await addTemporaryShelf(resourceListRef.value.getCheckRowKeys());
		resourceListRef.value.clearCheckRow();
	};

	const searchList = async (params?) => {
		listParams = params || listParams;
		return await queryResourceList(listParams);
	};

	const deleteResource = () => {};

	const getSelectRowLength = computed(() => {
		return resourceListRef?.value?.getCheckRowKeys().length;
	});

	onMounted(() => {});
</script>

<style lang="less" scoped>
	.table-toolbar-left {
		button {
			margin-right: 10px;
		}
	}
</style>
